/**
 * Created by isoft-work 5 on 1/18/2015.
 */
$(document).ready(function(){
   bindEventLogin();
});

function bindEventLogin() {
   ISS.EVENT.bindEvent('#AdLogin_Submit', 'click', function () {
      sendInfo();
   });

   ISS.EVENT.bindEvent('#AdLogin_Pass', 'keyup', function (e) {
      if(e.keyCode === CONSTANT.KEY_CODE.ENTER) {
         sendInfo();
      }
   });
}

function sendInfo() {
   var login = {};
//   get info
   login.email = ISS.DOM.getVal('#AdLogin_Username');
   login.pass = ISS.DOM.getVal('#AdLogin_Pass');
   if(login.email && login.pass) {
      //   request
      ISS.BACKEND.request({
         url: '../../service/admin/header/login',
         headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
         },
         type: 'POST',
         dataType: 'json',
         data: JSON.stringify(login),
         success: function (data) {
            var responseContent = data.responseObject;
            if(responseContent){
               loginComplete(responseContent);
            }
            else {
               loginError();
            }
         }
      });
   }
   else {
      alert('Hãy nhập đầy đủ thông tin');
   }
}

function loginComplete(login) {
   ISS.SESSION.setSession('userNameAdmin', login.email);
   window.location = SERVER.contextPath  + '/jsp/admin/ad-member-common.jsp';
}

function loginError() {
   alert('Tài khoản không hợp lệ');
}
